package com.titar.robot.ui.login;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.titar.robot.R;
import com.titar.robot.base.BaseActivity;
import com.titar.robot.entity.LoginResponse;
import com.titar.robot.entity.MemberInfo;
import com.titar.robot.ui.forgotpsd.ForgotPsdActivity;
import com.titar.robot.ui.main.MainActivity;
import com.titar.robot.ui.register.RegisterActivity;
import com.titar.robot.utils.ToastUtils;
import com.titar.robot.widgets.IconEditText;
import com.titar.robot.widgets.IconTextView;
import com.titar.robot.widgets.TntToolbar;


public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContact.View, View.OnClickListener {

    IconEditText mEtPhone;
    IconEditText mEtPsw;
    ImageView mBtnLogin;
    ImageView mTvRegist;
    TextView mTvForgetPassword;
    TextView login_version;

    private static final int REQUEST_NEW_PASSWORD = 100;// 找回密码
    public static final String USERNUM = "usernum";//账号
    public static final String PASSWORD = "password";//密码
    private TntToolbar mToolbar;
    private String nickName;
    private long id;

    @Override
    protected void onInitWidget(Bundle savedInstanceState) {
        mEtPhone = findViewById(R.id.et_phone);
        mEtPsw = findViewById(R.id.et_psw);
        mBtnLogin = findViewById(R.id.btn_login);
        mTvRegist = findViewById(R.id.tv_regist);
        mTvForgetPassword = findViewById(R.id.tv_forget_password);
        login_version = findViewById(R.id.login_version);

        mToolbar = (TntToolbar) findViewById(R.id.toolbar);
        mToolbar.getTitle().setText(getString(R.string.login));
        mToolbar.getIvLeft().setVisibility(View.GONE);
        mEtPsw.setEllipsize();

        mPresenter = new LoginPresenter().attachView(this);
        getLifecycle().addObserver(mPresenter);
        login_version.setText("Android软件版本:" + getVersionName(LoginActivity.this));

        iniDate();
    }

    private void iniDate() {
        if (getIntent() != null) {
            String user = getIntent().getStringExtra(USERNUM);
            String passw = getIntent().getStringExtra(PASSWORD);
            mEtPhone.setText(user);
        }
    }

    @Override
    protected void onInitListener() {
        mBtnLogin.setOnClickListener(this);
        mTvRegist.setOnClickListener(this);
        mTvForgetPassword.setOnClickListener(this);
    }

    @Override
    public int getContentViewId() {
        return R.layout.layout_login_activity;
    }

    @Override
    public void onLoginSucess(LoginResponse response) {
        //获取memberinfo
        mPresenter.getMemberInfo(response.getToken());
        //jump2ActivityAndFinish(MainActivity.class);
    }

    @Override
    public void onGetMemberInfoSucess(MemberInfo memberInfo) {
        jump2ActivityAndFinish(MainActivity.class);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                onClickLogin();
                break;

            case R.id.tv_regist:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);

                if (!TextUtils.isEmpty(mEtPhone.getText().toString())) {
                    intent.putExtra("phone", mEtPhone.getText().toString());

                }
                startActivity(intent);
                break;

            case R.id.tv_forget_password:
                jump2ActivityForResult(ForgotPsdActivity.class, REQUEST_NEW_PASSWORD);
                //   jump2Activity(UpdatePasswordActivity.class);
                break;
        }
    }

    /**
     * 点击登录
     */
    private void onClickLogin() {
        String phone = getPhoneNumber();
        String psw = getPsw();

        if (TextUtils.isEmpty(psw) || TextUtils.isEmpty(phone)) {
            ToastUtils.showToast(getString(R.string.phone_or_psw_can_not_null));
            return;
        }
        mPresenter.login(phone, psw);
    }

    public String getPhoneNumber() {
        return mEtPhone.getText().toString().trim();
    }


    public String getPsw() {
        return mEtPsw.getText().toString().trim();
    }
}
